﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BigLotterySyndicate.Classes;
using SyndicateDAO.DatabaseObject;

namespace BigLotterySyndicate.Secured.Payments
{
    public partial class Create : BasePage
    {
        protected int? UserId
        
        {
            get
            {
                if (Page.Request.QueryString["uId"] != null)
                {
                    int uId = 0;

                    Int32.TryParse(Page.Request.QueryString["uId"].ToString(), out uId);

                    return uId;
                }
                else
                    return null;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Visible)
                return;

            if (!this.IsUserLoggedIn)
                return;


            this.PageTitle += " | Add funds";
            this.MasterPage.SetTopMenuTab(global::BigLotterySyndicate.Secured.Controls.TopNav.AdminNavSection.Payments);

            SetUsers();
        }

        private void SetUsers()
        {
            if (Page.IsPostBack)
                return;

            var users = this.BusinessFacade.UserBL.GetAllBySyndicateId(this.LoggedInUser.syndicateId).OrderBy(u => u.username);

            ListItem item1 = new ListItem("Select", "0");
            this.ddlUsers.Items.Add(item1);


            foreach (user u in users)
            {
                ListItem item = new ListItem(u.username, u.id.ToString());

                this.ddlUsers.Items.Add(item);

            }

            if (this.UserId.HasValue && this.UserId.Value != 0)
                this.ddlUsers.SelectedValue = this.UserId.ToString();

        }

        protected void ButtonAdd_Click(object sender, EventArgs e)
        {
            if (!Page.IsValid)
                return;

            user u = this.BusinessFacade.UserBL.GetSingleUser(Int32.Parse(this.ddlUsers.SelectedValue));

            if (u != null)
            {
                long amount = Int64.Parse(this.txtAmount.Text);

                u.funds += amount;
                               

                if (this.BusinessFacade.UserBL.UpdateUser())
                {
                    fundsLog logEntry = new fundsLog();

                    

                    logEntry.fromUserId = this.LoggedInUser.id;
                    logEntry.toUserId = u.id;
                    logEntry.dateCreated = DateTime.Now;
                    
                    logEntry.syndicateId = this.LoggedInUser.syndicateId;

                    if (amount > 0)
                    {
                        logEntry.description = "Funds added to account.";
                        logEntry.moneyIn = true;
                        logEntry.amount = amount;
                    }
                    else
                    {
                        logEntry.description = "Funds removed from account.";
                        logEntry.moneyIn = false;
                        logEntry.amount = -amount;
                    }
                    

                    this.BusinessFacade.FundsLogBL.InsertEntry(logEntry);

                    InsertEventLogEntry("User [" + u.id.ToString() + "] funds updated.");

                    this.Redirect("/Secured/Payments/Default.aspx?Ns=3&payment=ok");

                }
                else
                {
                    this.Redirect("/Secured/Payments/Default.aspx?Ns=3&payment=fail");
                }


            }

        }
    }
}